package cc.idoone.spring_boot_hello.h2;

import cc.idoone.spring_boot_hello.h2.jpa.StaffBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @auther hemj
 * @since 2017/9/23
 */
@RestController
@RequestMapping("/h2")
public class StaffController {

	@Autowired
	@Qualifier("jpaStaffServiceImpl")
	private StaffService staffService;

	@RequestMapping("create")
	public String createTable() {
		staffService.createTable();

		return "ok";
	}

	@RequestMapping("save")
	public String save() {
		staffService.save();

		return "ok";
	}

	@RequestMapping("findAll")
	public List<StaffBo> findAll() {
		return staffService.findAll();
	}

	@RequestMapping("findByName")
	public StaffBo findByName() {
		return staffService.findByName();
	}

	@RequestMapping("findByUserNameIgnoreCase")
	public List<StaffBo> findByUserNameIgnoreCase() {
		return staffService.findByUserNameIgnoreCase("Abc");
	}

	@RequestMapping("findByUserIdAndUserNameAllIgnoreCase")
	public List<StaffBo> findByUserIdAndUserNameAllIgnoreCase() {
		return staffService.findByUserIdAndUserNameAllIgnoreCase("Abc", "Abc");
	}

	@RequestMapping("getByMinAge/{minAge}")
	public List<StaffBo> getByMinAge(@PathVariable int minAge) {
		return staffService.getByMinAge(minAge);
	}

	@RequestMapping("findByUserId")
	public StaffBo findByUserId(@RequestParam String id) {
		return staffService.findByUserId(id);
	}

	@RequestMapping("findByAgeGreaterThan")
	public Page<StaffBo> findByAgeGreaterThan(@RequestParam Integer age) {
		Pageable pageable = new Pageable() {
			@Override
			public int getPageNumber() {
				return 0;
			}

			@Override
			public int getPageSize() {
				return 2;
			}

			@Override
			public int getOffset() {
				return 0;
			}

			@Override
			public Sort getSort() {
				return null;
			}

			@Override
			public Pageable next() {
				return null;
			}

			@Override
			public Pageable previousOrFirst() {
				return null;
			}

			@Override
			public Pageable first() {
				return null;
			}

			@Override
			public boolean hasPrevious() {
				return false;
			}
		};
		return staffService.findByAgeGreaterThan(age, pageable);
	}

	@RequestMapping("increaseAge")
	public int increaseAge(@RequestParam int after, @RequestParam int before) {
		return staffService.increaseAge(after, before);
	}
}
